package com.bawei.module_classify

import android.util.Log
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.library_base.RouterPath
import com.bawei.library_base.mvp.v.BaseFragment
import com.bawei.module_classify.mvp.*
import com.chad.library.adapter.base.listener.OnItemClickListener
import kotlinx.android.synthetic.main.fragment_classify.*


/**
 * @author : zhiyu
 * @date : 2021/12/14
 * @content :
 */
@Route(path = RouterPath.Classify.PAGER_CLASSIFY)
class ClassifyFragment : BaseFragment<GoodPresenter>(), GoodContract.GoodView {

    lateinit var goodAdapter: GoodAdapter

     var goodsAdapter: GoodsAdapter= GoodsAdapter(null)

    override fun beanLayout(): Int {
        return R.layout.fragment_classify
    }

    override fun initView() {


        //左
        classify_vertical_rv.layoutManager=LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)

        //右
        classify_horizontal_rv.setLayoutManager(
            StaggeredGridLayoutManager(
                3,
                StaggeredGridLayoutManager.VERTICAL
            )
        )

    }

    override fun initData() {
        mPresenter= GoodPresenter(GoodsModel(),this)
        mPresenter.getGoods(0)
    }
    override fun initAdapter(databases: List<Data>) {

        for (i in 0 until databases.size) {
            val categoryName: String = databases.get(i).category_name
            if (categoryName.contains("/")) {
                var substring = categoryName.substring(0, categoryName.indexOf("/"))
                databases.get(i).category_name=substring.toString()
            }
        }

        goodAdapter= GoodAdapter(databases)
        Log.i("22222222TAG", "initAdapter: "+databases)
//        goodAdapter.data.addAll(databases)
        classify_vertical_rv.adapter=goodAdapter

       goodAdapter.setOnItemChildClickListener { adapter, view, position ->
//           mPresenter.getGoods(16)


           when(view.id){
                R.id.tv_type->  {
                    if (goodsAdapter!=null){
                        goodsAdapter.data.clear()
                        goodsAdapter.notifyDataSetChanged();
                    }
                    mPresenter.getvGoods(goodAdapter.data.get(position).category_id)

           }

           }
       }


    }

    override fun initsAdapter(databases: List<Data>) {
        for (i in 0 until databases.size) {
            val categoryName: String = databases.get(i).category_name
            if (categoryName.contains("/")) {
                var substring = categoryName.substring(0, categoryName.indexOf("/"))
                databases.get(i).category_name=substring.toString()
            }
        }

            goodsAdapter=GoodsAdapter(databases)

            classify_horizontal_rv.adapter=goodsAdapter
        goodsAdapter.setOnItemClickListener(OnItemClickListener { adapter, view, position ->
            ARouter.getInstance()
                .build(RouterPath.SearchGoods.SEARCH_GOODS)
                .withString("name",databases.get(position).category_name)
                .withBoolean("ok",true)
                .navigation()
        })

    }
}